/*
 * @description: Token 控制器
 * @Date: 2023-05-12 17:02:46
 * @example: 
 * @params: 
 */
const express = require('express')
const Router = express.Router()
const expressJwt = require('express-jwt').expressjwt


Router.use(expressJwt({secret:'zhangkai', algorithms:['HS256']}).unless({
    path: ['/create-tenant','/getSpaceList','/test','/LoginSync','/Captcha','/Emails', '/UpdatePass', '/CheckUser', '/swagger','/swagger/swagger-ui.css','/swagger/swagger-ui-bundle.js','/swagger/swagger-ui-standalone-preset.js','/swagger/favicon-32x32.png','/swagger.json'],
}))

// 将租户id挂载请求头放入中间件
Router.use((req, res, next) => { 
    // Set up multi-tenant middleware 
    const tenantId = req.headers['x-tenant-id'];
    req.tenantId = tenantId;
    next();
  });
// 挂载 登录模块  路由表
Router.use(require('./login/index'))
Router.use(require('./ImageUpload/index'))
Router.use(require('./project/index'))
// 处理错误
Router.use((err, req, res, next)=>{
    let errMsg = {
        code:401,
        message:'invalid token 还未登录哦 ...'
    }
    if (err.name == 'UnauthorizedError') {
        res.send(errMsg)
        return
    }
    errMsg.message = err.name
    res.send(errMsg)
    return
    next()
})

module.exports = Router